#include "ui.h"
#include "lcd.h"
#include "math.h"
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "delay.h"
#define Pi 3.1415926
/* FIFO */
void Data_FIFO(u16 Data_in, u16 *para, u16 len)
{
    u16 i;
    for (i = len - 1; i > 0; i--)
    {
        para[i] = para[i - 1];
    }
    para[0] = Data_in;
}

void Set_Display_wave()
{
    Display.wave = Sine;
    Display.Freq = 1000;
    Display.Amplitude = 1;
}

void LCD_Task(void *pvParameters)
{
    char buff[3] = {0};
    static u16 ware_data[234] = {0};
    static int data_temp = 0, x = 0;
    static u16 len = sizeof(ware_data) / sizeof(u16);
    static u8 dir = 0;
    u16 i = 0;
    u16 imagLine_x, imagLine_y;
    while (1)
    {
        x++;

        if (x >= 4800)
            x = 0;

        for (imagLine_x = 2; imagLine_x <= 236; imagLine_x++)
        {
            if (imagLine_x % 30 == 0)
                LCD_Draw_imagLine(imagLine_x, 2, imagLine_x, 236);
        }
        for (imagLine_y = 2; imagLine_y <= 236; imagLine_y++)
        {
            if (imagLine_y % 30 == 0)
                LCD_Draw_imagLine(2, imagLine_y, 236, imagLine_y);
        }
        LCD_DrawLine(2, 120, 237, 120, GREEN);

        if (Display.out_0 == OFF)
            Display.Amplitude = 0;

        if (Display.wave == Sine)
        {
            data_temp = 2*Display.Amplitude * sin(2 * Pi * 0.00625 * x) + 120;
            Data_FIFO(data_temp, ware_data, len);
            for (i = 4; i < 235; i++)
            {
                if (ware_data[i] >= 2)
                {
                    LCD_Fast_DrawPoint(i, ware_data[i - 1], LIGHTBLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i - 1] + 1, LIGHTBLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i - 1] - 1, LIGHTBLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i - 2], BLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i - 2] + 1, BLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i - 2] - 1, BLUE);
                }
            }
        }
        else if (Display.wave == Square)
        {
            if (x % 60 == 0)
            {
                dir = !dir;
                if (dir == 0)
                {
                    data_temp = 120 - 2*Display.Amplitude;
                }
                else
                {
                    data_temp = 120 + 2*Display.Amplitude;
                }
            }
            Data_FIFO(data_temp, ware_data, len);
            for (i = 4; i < 235; i++)
            {
                if (ware_data[i] >= 2)
                {
                    LCD_Fast_DrawPoint(i, ware_data[i], BLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i] + 1, BLUE);
                    LCD_Fast_DrawPoint(i, ware_data[i] - 1, BLUE);
                    if (ware_data[i] != ware_data[i - 1])
                    {
                        LCD_DrawLine(i, ware_data[i] + 1, i, ware_data[i - 1] - 1, BLUE);
                        LCD_DrawLine(i, ware_data[i] - 1, i, ware_data[i - 1] + 1, BLUE);
                        LCD_DrawLine(i - 1, ware_data[i] + 1, i - 1, ware_data[i - 1] - 1, LIGHTBLUE);
                        LCD_DrawLine(i - 1, ware_data[i] - 1, i - 1, ware_data[i - 1] + 1, LIGHTBLUE);
                    }
                }
            }
        }
        else if (Display.wave == Triangle)
        {
            if (x % 60 == 0)
            {
                dir = !dir;
            }
            if (dir == 0) //���ǲ�������
            {
                data_temp = 120 + (float)(2.0 * 2*Display.Amplitude / 60.0) * (x % 60 - 30);
            }
            else //���ǲ��½���
            {
                data_temp = 120 + (float)(-2.0 * 2*Display.Amplitude / 60.0) * (x % 60 - 30);
            }
            Data_FIFO(data_temp, ware_data, len);
            for (i = 4; i < 235; i++)
            {
                LCD_Fast_DrawPoint(i, ware_data[i - 1], LIGHTBLUE);
                LCD_Fast_DrawPoint(i, ware_data[i - 1] + 1, LIGHTBLUE);
                LCD_Fast_DrawPoint(i, ware_data[i - 1] - 1, LIGHTBLUE);
                LCD_Fast_DrawPoint(i, ware_data[i - 2], BLUE);
                LCD_Fast_DrawPoint(i, ware_data[i - 2] + 1, BLUE);
                LCD_Fast_DrawPoint(i, ware_data[i - 2] - 1, BLUE);
            }
        }
        LCD_DrawLine(233, 2, 233, 236, LIGHTBLUE);

        if (Display.out_0 != Display.out_1)
        {
            if (Display.out_0 == ON)
            {
                LCD_Fill(242, 112, 317, 128, GREEN);
                LCD_ShowString(274, 112, 74, 16, 16, BLACK, (u8 *)"ON");
            }
            else if (Display.out_0 == OFF)
            {
                LCD_Fill(242, 112, 317, 128, RED);
                LCD_ShowString(271, 112, 74, 16, 16, BLACK, (u8 *)"OFF");
            }
            Display.out_1 = Display.out_0;
        }
        if (Display.CPU_usage >= 100)
            Display.CPU_usage = 100;
        LCD_ShowString(165, 2, 36, 12, 16, BLACK, (u8 *)"CPU:");
        sprintf(buff, "%d%%", Display.CPU_usage);
        LCD_Fill(200, 2, 230, 16, LIGHTBLUE);
        LCD_ShowString(200, 2, 36, 12, 16, BLACK, (u8 *)buff);
   
        sprintf(buff, "%.2fV", AD9833.Amplitude);
        LCD_Fill(243 + 10 + 10, 50, 310 , 64, LIGHTBLUE);
        LCD_ShowString(243 + 10 + 10, 50, 74, 16, 16, BLACK, (u8 *)buff);

        if(AD9833.freq<1000)
        sprintf(buff, "%.1fHz", AD9833.freq);
        else if(AD9833.freq>=1000&&AD9833.freq<1000000)
        sprintf(buff, "%.3fkHz", AD9833.freq/1000.0);
        LCD_Fill(242, 50+130, 273 + 10 + 10+20+3, 64+130, LIGHTBLUE);
        LCD_ShowString(242, 50+130, 74, 16, 16, BLACK, (u8 *)buff);

        vTaskDelay(Display.wave_speed);
    }
}

void UI_black()
{
    u16 x, y;
    LCD_DrawRectangle(0, 0, 238, 238, YELLOW);
    LCD_DrawRectangle(1, 1, 237, 237, YELLOW);
    LCD_DrawLine(239, 0, 239, 240, BLACK);
    LCD_DrawRectangle(240, 0, 319, 239, YELLOW);
    LCD_DrawRectangle(241, 1, 318, 238, YELLOW);

    LCD_DrawRectangle(242, 119 + 10, 317, 119 + 10, YELLOW);
    LCD_DrawRectangle(239, 120 + 10, 319, 120 + 10, BLACK);
    LCD_DrawRectangle(242, 121 + 10, 317, 121 + 10, YELLOW);

    LCD_DrawRectangle(242, 119 - 10, 317, 119 - 10, YELLOW);
    LCD_DrawRectangle(239, 120 - 10, 319, 120 - 10, BLACK);
    LCD_DrawRectangle(242, 121 - 10, 317, 121 - 10, YELLOW);

       LCD_ShowString(243, 2, 74, 16, 16, BLACK, "Amplitude");
    LCD_ShowString(243, 132, 74, 16, 16, BLACK, "Frequency");

    for (x = 2; x <= 237; x++)
    {
        if (x % 30 == 0)
            LCD_Draw_imagLine(x, 2, x, 237);
    }
    for (y = 2; y <= 237; y++)
    {
        if (y % 30 == 0)
            LCD_Draw_imagLine(2, y, 237, y);
    }
    LCD_DrawLine(2, 120, 237, 120, GREEN);
}

void UI_start(void)
{
    int i;
    LCD_Clear(BLACK);
    for (i = 0; i < 5; i++)
    {
        LCD_Fill(228, 110, 260, 140, BLACK);
        LCD_ShowString(60, 110, 240, 60, 24, WHITE, "system initial   ");
        delay_ms(200);
        LCD_ShowString(60, 110, 240, 60, 24, WHITE, "system initial.  ");
        delay_ms(200);
        LCD_ShowString(60, 110, 240, 60, 24, WHITE, "system initial.. ");
        delay_ms(200);
        LCD_ShowString(60, 110, 240, 60, 24, WHITE, "system initial...");
        delay_ms(200);
    }
}
